import { nanoid } from "nanoid";
import Store from "store";
// 数据库名称
const DB_NAME = "bid_user_info_list";

export const register = (params) => {
  const list = JSON.parse(window.localStorage.getItem(DB_NAME));
  const data = {
    ...list,
    ...params,
  };
  window.localStorage.setItem(DB_NAME, JSON.stringify(data));
  return { code: 0, msg: "注册成功", data: {} };
};

// 获取所有用户信息
export const getUserAll = () => {
  let code = 0;
  let msg = "登录成功";
  const data = JSON.parse(window.localStorage.getItem(DB_NAME)) || {};

  const arr = [];

  Object.keys(data).forEach((e) => {
    arr.push(data[e]);
  });
  return { code, data: arr, msg };
};

// 删除用户信息
export const deleteUserInfo = (name) => {
  let code = 0;
  let msg = "获取成功";
  const data = JSON.parse(window.localStorage.getItem(DB_NAME)) || {};

  const arr = [];
  Object.keys(data).forEach((e) => {
    if (data[e].username === name) {
      delete data[e];
    } else {
      arr.push(data[e]);
    }
  });

  window.localStorage.setItem(DB_NAME, JSON.stringify(data));
  return { code, data: arr, msg };
};

export const userLogin = ({ username, password }) => {
  let code = 0;
  let msg = "登录成功";
  const list = JSON.parse(window.localStorage.getItem(DB_NAME)) || {};

  const user = list[username];
  console.log(user);
  if (!user) {
    code = 500;
    msg = "账号不存在";
    return { code, data: { ...user }, msg };
  }
  if (password !== user.password) {
    code = 500;
    msg = "密码错误";
  }
  return { code, data: { ...user }, msg };
};

// 数据库名称
const DB_NAME_REQ = "bid_require_list";

export const makeFakeData = () => {
  const reqTypeList = ["工程类", "土木类", "民用建筑工程类", "土石方工程类"];
  const proNameList = ["精装修", "建楼", "民用建筑", "提供土石方原料"];
  const proTypeList = ["提供原材料", "建楼", "装修"];
  const proCodeList = ["20211212", "20220101", "20211213", "20211214"];
  const bugTypeList = ["建筑类别", "建造类别", "装修类别", "原材料类别"];
  const prePriceList = ["100000", "14233000", "42341555", "4340049", "512351344"];
  const timeReqList = ["2022-7-13", "2022-9-13", "2022-7-11"];
  const bidCompanyList = ["腾讯", "阿里巴巴", "万科", "米哈游", "重生"];
  const busReqList = ["给建筑方提供建楼原材料", "满足人们物质和文化生产性建筑物", "符合即可"];
  const reqCauseList = ["建写字楼", "大面积装修", "建筑民用建筑", "原材料提供"];
  const myRandom = (fakeList) => {
    const num = Math.floor(Math.random() * fakeList.length);
    return fakeList[num];
  };
  const randomScore = () => {
    const num = Math.floor(Math.random() * 10) + 1;
    return num;
  };
  let list = [];
  for (let i = 0; i < 10; i++) {
    const reqList = {
      id: nanoid(),
      reqType: myRandom(reqTypeList),
      proName: myRandom(proNameList),
      proType: myRandom(proTypeList),
      proCode: myRandom(proCodeList),
      bugType: myRandom(bugTypeList),
      prePrice: myRandom(prePriceList),
      buyRange: "",
      timeReq: myRandom(timeReqList),
      busReq: myRandom(busReqList),
      reqCause: myRandom(reqCauseList),
      bidUser: Store.state.userInfo.username,
      bidCompany: myRandom(bidCompanyList),
      expScore: randomScore(),
      bidPriceScore: randomScore(),
      executeScore: randomScore(),
      status: 2,
    };
    if (reqList.status === 2) {
      reqList.company = [myRandom(bidCompanyList)];
      reqList.bidUser = ["李热望"];
      const fileList = [
        '2018年招投标规章制度.docx',
        '2019年招投标管理细则.docx',
        '2021年招投标详情.docx',
        '2022年申请投标制度.docx',
        '招投标管理文件.docx',
      ];

      reqList.fileList = {
        [reqList.company]: myRandom(fileList),
      };
    }
    if (reqList.status === 4) {
    }
    list.push(reqList);
  }
  const local = JSON.parse(window.localStorage.getItem(DB_NAME_REQ)) || {};
  // 给本地缓存新增数据
  const value = local[Store.state.userInfo.username] || {};
  // 如果本地有假数据那么不继续随机， 让原来的数据覆盖
  const res = {
    [Store.state.userInfo.username]: { data: [...(value.data || []), ...list] },
    ...local,
  };
  window.localStorage.setItem(DB_NAME_REQ, JSON.stringify(res));
};
